package com.itany.conf;

import com.itany.interceptor.LogInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.*;

import java.io.FileNotFoundException;

@Configuration
public class TestMvcConfig implements WebMvcConfigurer {


    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/showerror").setViewName("error");
        registry.addViewController("/showlogin").setViewName("login");
        registry.addViewController("/showindex").setViewName("index");
        registry.addViewController("/showbooks").setViewName("booklist");
        registry.addViewController("/showusers").setViewName("userlist");
        registry.addViewController("/showorders").setViewName("orderlist");
        registry.addViewController("/showprovinces").setViewName("province");
        registry.addViewController("/showcities").setViewName("city");
        registry.addViewController("/showaddresses").setViewName("region");
        registry.addViewController("/showbooktypes").setViewName("booktype");
        registry.addViewController("/showsupplierbooks").setViewName("supplierbooklist");
        registry.addViewController("/showsuppliers").setViewName("supplierlist");
        registry.addViewController("/supplierExamine").setViewName("supplier_examine");
        registry.addViewController("/supplierPlatform").setViewName("supplier_platform");
        registry.addViewController("/permission").setViewName("permission");
        registry.addViewController("/role").setViewName("role");
        registry.addViewController("/manager").setViewName("manager");

    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String cp = null;
        try {

          cp  = ResourceUtils.getURL("classpath:static").getPath() +"/upload/";
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        registry.addResourceHandler("/upload/**").addResourceLocations("file:"+cp);
    }


    // 配置拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 定义拦截器是谁
        registry.addInterceptor(new LogInterceptor())
                // 拦截谁,值可以是可变长参数,也可以是字符串集合
                .addPathPatterns("/showindex")
                .addPathPatterns("/showusers")
                .addPathPatterns("/showorders")
                .addPathPatterns("/showprovinces")
                .addPathPatterns("/showcities")
                .addPathPatterns("/showaddresses")
                .addPathPatterns("/showbooktypes")
                .addPathPatterns("/showbooks")
                .addPathPatterns("/supplierExamine")
                .addPathPatterns("/supplierPlatform")
                .addPathPatterns("/showsupplierbooks")
                .addPathPatterns("/showsuppliers")
                .addPathPatterns("/permission")
                .addPathPatterns("/role")
                .addPathPatterns("/manager") //之前为页面的请求拦截
                .addPathPatterns("/book/*")
                .addPathPatterns("/examine/*")
                .addPathPatterns("/manager/*")
                .addPathPatterns("/permission/*")
                .addPathPatterns("/press/*")
                .addPathPatterns("/role/*")
                .addPathPatterns("/supplier/*")
                .addPathPatterns("/user/*") //之前为controller的请求拦截
                // 不拦截谁,值可以是可变长参数,也可以是字符串集合
                .excludePathPatterns("/manager/login")
                .excludePathPatterns("/showlogin")
                .excludePathPatterns("/showerror");
    }
}
